package com.maven.plugin;
import java.io.File;
import java.io.Serializable;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

/**
 * 
 * @Author Huang
 * @CreationTime 2017年5月1日
 */
@Mojo(name = "MavenPlugin", defaultPhase = LifecyclePhase.PACKAGE)
public class MavenPlugin extends AbstractMojo implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5L;
	private static final String JAVA_FILE_TYPE = ".java";
	
	@Parameter(property = "fullFileName")
	private String fullFileName;
	
	public void execute() throws MojoExecutionException, MojoFailureException {
		File file = new File(fullFileName);
		System.out.println("该路径下文件数量：" + countAllFiles(file));
	}

	public int countAllFiles(File dir) {
		int num = 0;
		if (dir != null) {
			File[] fs = dir.listFiles();
			if (ArrayUtils.isNotEmpty(fs)) {
				for (int i = 0; i < fs.length; i++) {
					if (fs[i] != null) {
						if (fs[i].isFile() && fs[i].getAbsolutePath().indexOf(JAVA_FILE_TYPE) > 0) {
							num += 1;
						}
						if (fs[i].isDirectory()) {
							num += countAllFiles(fs[i]);
						}
					}
				}
			}
		}
		return num;
	}
}
